<?php
/**
 * Created by PhpStorm.
 * User: iceberg
 * Date: 2018-05-20
 * Time: 15:22
 */

namespace app\behavior;

use think\Request;
use traits\controller\Jump;
class CheckAuth
{
    use Jump;  //引入trait
    public function run(Request $request, $params)
    {
        //获取 model/controller/action
        $path = strtolower($request->module()) . '/' . strtolower($request->controller()) . '/' . strtolower($request->action());
        $pathController = strtolower($request->module()) . '/' . strtolower($request->controller());
        //echo $path;
        //dump(in_array($path,config('except.other')));
        //排除的地址 放行
        if(in_array($path,config('rbac.OTHER')) == false)
        {
            //判断用户是否登录
            $role_id = session('role_id');
            if($role_id) {
                //当用户不是超级管理员的时候进行权限判断
                if($role_id > 1){
                    $rbac_role_auths = config('rbac.RBAC_ROLE_AUTHS')[$role_id];
                    //dump($rbac_role_auths);die;
                    if(!in_array($path, $rbac_role_auths) && !in_array($pathController . '/*', $rbac_role_auths))
                    {
                        //用户没有权限
                        $this->error('您没有权限!','admin/Sign/login');
                    }
                }
            }else{
                //没有登录则跳转登录
                return $this->redirect('/admin/sign/login');
            }

        }

    }
}